package com.exonline.commons.utils;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;





public class RequestBodyConverter {
	
	
	private static final ObjectMapper objectMapper = new ObjectMapper();

    public static <T> T convertRequestBody(HttpServletRequest request, Class<T> targetClass) throws IOException {
        // 读取请求体内容
        StringBuilder requestBody = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String line;
        while ((line = reader.readLine())!= null) {
            requestBody.append(line);
        }

        // 将请求体内容转换为指定的Java对象
        return objectMapper.readValue(requestBody.toString(), targetClass);
    }
	

}
